import util.GuiImg;
import util.FileSerialization;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class Atm extends JFrame implements ActionListener {
    CardLayout cardLayout = new CardLayout();
    private Image atmImg = GuiImg.getImage("/imgs/background.jpg");

    private Check check = new Check(this);
    private Login login = new Login(this);
    private Menu menu = new Menu(this);
    private Deposit deposit = new Deposit(this);
    private Get get = new Get(this);
    private Alter alter = new Alter(this);
    private Transfer transfer = new Transfer(this);
    private static Atm atm = new Atm();
    private int index;
    private User loginuser;

    public Atm() {
        this.setContentPane(new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.drawImage(atmImg, 0, 0, 800, 600, this);
            }
        });
        this.setLayout(cardLayout);
        //登录
        this.add("登录", login);
        //菜单
        this.add("菜单", menu);
        //存款
        this.add("存款", deposit);
        //取款
        this.add("取款", get);
        //查询
        this.add("查询", check);
        //改密
        this.add("改密", alter);
        //转账
        this.add("转账", transfer);

        //界面初始化
        this.setSize(800, 600);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }

    public static void main(String[] args) {
        new Atm();
    }

    public void switchPanel(String name) {
        cardLayout.show(this.getContentPane(), name);
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public Check getCheck() {
        return check;
    }

    public void setCheck(Check check) {
        this.check = check;
    }

    public Deposit getDeposit() {
        return deposit;
    }

    public void setDeposit(Deposit deposit) {
        this.deposit = deposit;
    }

    public Menu getMenu() {
        return menu;
    }

    public void setMenu(Menu menu) {
        this.menu = menu;
    }

    public User getLoginuser() {
        return loginuser;
    }

    public void setLoginuser(User loginuser) {
        this.loginuser = loginuser;
    }

    //数据库连接
    public Connection getConnection() {
        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/z725?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "z6787526");
        } catch (ClassNotFoundException | SQLSyntaxErrorException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    //修改
    public void setUserPasswordAndMoney(String id, String password, Double money) {
        Connection con = getConnection();
        Statement statement = null;
        try {
            statement = con.createStatement();
            String sql = "update t_user set a_password='" + password + "',a_money=" + money + " where a_id='" + id + "'";
            int row = statement.executeUpdate(sql);
            System.out.println("影响的行数:" + row);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4.关闭连接
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //获取
    public User getUser(String id, String password) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from t_user where a_id=? and a_password=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            statement.setObject(2, password);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new User();
                user.setId(rs.getString("a_id"));
                user.setPassword(rs.getString("a_password"));
                user.setMoney(rs.getDouble("a_money"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4.关闭连接
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return user;
    }

    //id获取
    public User getUserid(String id) {
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;
        User user = null;
        String sql = "select * from t_user where a_id='" + id + "'";
        try {
            statement = con.createStatement();
            rs = statement.executeQuery(sql);
            if (rs.next()) {
                user = new User();
                user.setId(rs.getString("a_id"));
                user.setPassword(rs.getString("a_password"));
                user.setMoney(rs.getDouble("a_money"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //4.关闭连接
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return user;
    }

    //转账事务
    public void zhuanzhang(User thisUser, User otherUser) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        String sql = "update t_user set a_money=? where a_id=?";
        try {
            con.setAutoCommit(false);
            statement = con.prepareStatement(sql);
            statement.setObject(1, thisUser.getMoney());
            statement.setObject(2, thisUser.getId());
            System.out.println("1");
            statement.executeUpdate();

            statement = con.prepareStatement(sql);
            statement.setObject(1, otherUser.getMoney());
            statement.setObject(2, otherUser.getId());
            statement.executeUpdate();
            System.out.println("2");
            con.commit();
            System.out.println("3");
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } finally {
            //4.关闭连接
            if (statement != null) {
                try {
                    statement.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
